package com.bieber.democamp.sharesession.session;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * Created by bieber on 2015/3/16.
 */
public abstract class AbstractSessionManager implements SessionManager {



    protected String getSessionIdFromCookie(HttpServletRequest request){
        Cookie[] cookies= request.getCookies();
        for(Cookie cookie:cookies){
            if(cookie.getName().equals(SESSION_COOKIE_KEY)){
                return cookie.getValue();
            }
        }
        return null;
    }

    protected String generateSessionId(){
        return UUID.randomUUID().toString();
    }

    protected int getSessionExpire(HttpServletRequest request){
        Object sessionExpire = request.getServletContext().getInitParameter(SESSION_EXPIRE_KEY);
        if(sessionExpire==null){
            return DEFAULT_SESSION_EXPIRE;
        }
        return Integer.parseInt(sessionExpire.toString().trim());
    }
}
